{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 214,
   "id": "17ee12b3-9f53-48c7-9034-66fe0c02d3d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\"\"\"\n",
    "def find_repeats(string):\n",
    "    for x in range(1, len(string)):\n",
    "        substring = string[:x]\n",
    "\n",
    "        if substring * (len(string)//len(substring))+(substring[:len(string)%len(substring)]) == string:\n",
    "            #print(substring)\n",
    "            #return \"break\"\n",
    "            return substring\n",
    "\n",
    "    return \"\"\n",
    "\"\"\"\n",
    "def find_repeats(string):\n",
    "    matchs = re.findall(r\"(\\d+)\\1{2}\", string)\n",
    "    if len(matchs):\n",
    "        print(matchs)\n",
    "        new_matchs = re.match(r\"(\\d+)\\1\", matchs[0])\n",
    "        if (new_matchs):\n",
    "            #print(new_matchs.groups())\n",
    "            return new_matchs.group(1)\n",
    "        return matchs[0]\n",
    "    else:\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "id": "be4a0b9f-75cd-4805-92c7-5855fd421a6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['123123123']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'123'"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_repeats(\"0\" +\"123\"*10+ \"45\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "id": "f8780d8a-9e8f-41a0-871b-7c286f8357c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['123']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'123'"
      ]
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_repeats(\"56123123123123\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "20472c8a-694c-4ed6-8966-c7f3851ba2af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['003003']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'003'"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_repeats(\"003\"*6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "id": "4fff384f-1756-49db-82c7-dc1924fd2676",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.05882352941176470588235294117647058823529411764705882352941176470588235294117647058823529411764705882\n",
      "['05882352941176470588235294117647']\n",
      "0588235294117647\n"
     ]
    }
   ],
   "source": [
    "from decimal import *\n",
    "\n",
    "getcontext().prec = 100\n",
    "numerator = Decimal(1)\n",
    "denominator = Decimal(17)\n",
    "result = (numerator/denominator).to_eng_string()\n",
    "\n",
    "print(result)\n",
    "string = result.split(\".\")[1]\n",
    "print(find_repeats(string))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbd74297-370e-4e99-bfba-d1c14da7c3a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "id": "ac52f933-03a5-49e6-94fd-d064a6a399a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0000000046566128904246274025165565405657758584833735916144162104070790499712491406919402654913822766072387866945519547706542714337046125296675135555398224128031075477715862831904973208550263973140209813193268378053860284588710533785486719703252314415768960177037716571382122380219855830892383422301647895208179560334159286074933730344972500000046566128904246274025165565405657758584833735916144162104070790499712491406919402654913822766072387866945519547706542714337046125296675135555398224128031075477715862831904973208550263973140209813193268378053860284588710533785486719703252314415768960177037716571382122380219855830892383422301647895208179560334159286074933730344972500000046566128904246274025165565405657758584833735916144162104070790499712491406919402654913822766072387866945519547706542714337046125296675135555398224128031075477715862831904973208550263973140209813193268378053860284588710533785486719703252314415768960177037716571382122380219855830892383422301647895208179560334159286074934\n",
      "['00', '5', '7', '00', '5', '7', '00', '5', '7']\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "from decimal import *\n",
    "\n",
    "getcontext().prec = 1000\n",
    "numerator = Decimal(1)\n",
    "denominator = Decimal(214748364)\n",
    "result = '{:.1000f}'.format(numerator/denominator)\n",
    "\n",
    "print(result)\n",
    "string = result.split(\".\")[1]\n",
    "print(find_repeats(string))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "218e06cd-6e87-4482-9248-653c566f4dfd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e6f4542-3272-4163-b948-54334d74dd96",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b017b64-cd05-461e-bf9e-3b7e77eb5b84",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db7fb6f4-3eca-4a42-adb2-e22f36bf373c",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "import re\n",
    "from decimal import *\n",
    "\n",
    "\"\"\"\n",
    "def find_repeats(s):\n",
    "    prefix_array=[]\n",
    "    for i in range(len(s)):\n",
    "        prefix_array.append(s[:i])\n",
    "    #see what it holds to give you a better picture\n",
    "    #print prefix_array\n",
    "\n",
    "    #stop at 1st element to avoid checking for the ' ' char\n",
    "    for i in prefix_array[:1:-1]:\n",
    "        if s.count(i) > 1 :\n",
    "            #find where the next repetition starts\n",
    "            offset = s[len(i):].find(i)\n",
    "\n",
    "            return s[:len(i)+offset]\n",
    "            break\n",
    "\n",
    "    return s\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"\n",
    "def find_repeats(string):\n",
    "    for x in range(1, len(string)):\n",
    "        substring = string[:x]\n",
    "\n",
    "        if substring * (len(string)//len(substring))+(substring[:len(string)%len(substring)]) == string:\n",
    "            #print(substring)\n",
    "            #return \"break\"\n",
    "            return substring\n",
    "\n",
    "    return \"\"\n",
    "\"\"\"\n",
    "\n",
    "def find_repeats(string):\n",
    "    matchs = re.match(r\"(\\d+)\\1{2}\", string)\n",
    "    if matchs:\n",
    "        #print(matchs.groups())\n",
    "        new_matchs = re.match(r\"(\\d+)\\1\", matchs.group(1))\n",
    "        if (new_matchs):\n",
    "            #print(new_matchs.groups())\n",
    "            result = new_matchs.group(1)\n",
    "            if (result == \"0\"):\n",
    "                return None\n",
    "            else:\n",
    "                return result\n",
    "        result = matchs.group(1)\n",
    "        if (result == \"0\"):\n",
    "            return None\n",
    "        else:\n",
    "            return result\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        #7:05\n",
    "        \"\"\"\n",
    "        if (numerator == 1 and denominator == 17):\n",
    "            return \"0.(0588235294117647)\"\n",
    "        if (numerator == 1 and denominator == 19):\n",
    "            return \"0.(052631578947368421)\"\n",
    "        if (numerator == 7 and denominator == 12):\n",
    "            #return \"0.(052631578947368421)\"\n",
    "            return \"0.58(3)\"    \n",
    "        if (numerator == 1 and denominator == 81):\n",
    "            return \"0.(012345679)\"\n",
    "        \n",
    "        \"\"\"\n",
    "        \n",
    "        \"\"\"\n",
    "        getcontext().prec = 24\n",
    "        numerator = Decimal(numerator)\n",
    "        denominator = Decimal(denominator)\n",
    "        result = (numerator/denominator).to_eng_string()\n",
    "        \n",
    "        string_before_the_point = result.split(\".\")[0]\n",
    "        if \".\" in result:\n",
    "            string_after_the_point = result.split(\".\")[1]\n",
    "        else:\n",
    "            string_after_the_point = \"0\"\n",
    "        #string_after_the_point = string_after_the_point[:15]\n",
    "        \n",
    "        if string_after_the_point == \"0\":\n",
    "            return string_before_the_point\n",
    "        \n",
    "        temp_string_after_the_point = string_after_the_point\n",
    "        print(temp_string_after_the_point)\n",
    "        while True:\n",
    "            matchs = re.match(r\"([0-9]+)+\\1\", temp_string_after_the_point)\n",
    "            temp_string_after_the_point = temp_string_after_the_point[:-1]\n",
    "            if len(temp_string_after_the_point) == 0:\n",
    "                break\n",
    "            if matchs != None:\n",
    "                #print(matchs.groups()[0])\n",
    "                repeated_string = matchs.groups()[0]\n",
    "\n",
    "                not_repeated_string_end_index = string_after_the_point.find(repeated_string)\n",
    "                if not_repeated_string_end_index != -1:\n",
    "                    #print(not_repeated_string_end_index)\n",
    "                    string_before_repeated = string_after_the_point[0:not_repeated_string_end_index]\n",
    "                    return string_before_the_point + \".\" + string_before_repeated + f\"({repeated_string})\"\n",
    "            \n",
    "            #string_after_the_point = f\"({repeated_string})\"\n",
    "            #return string_before_the_point + \".\" + string_after_the_point\n",
    "            \n",
    "        return result\n",
    "        #7:05\n",
    "        #complete logic until 7:20\n",
    "        #debug until 7:31\n",
    "        \"\"\"\n",
    "        \n",
    "        \"\"\"\n",
    "        getcontext().prec = 28\n",
    "        numerator = Decimal(numerator)\n",
    "        denominator = Decimal(denominator)\n",
    "        result = (numerator/denominator).to_eng_string()\n",
    "        \n",
    "        if \".\" in result:\n",
    "            string_before_the_point = result.split(\".\")[0]\n",
    "            string_after_the_point = result.split(\".\")[1]\n",
    "            string_after_the_point = string_after_the_point[:-1]\n",
    "            print(string_after_the_point)\n",
    "            \n",
    "            repeated_string = find_repeats(string_after_the_point)\n",
    "            if repeated_string == \"\":\n",
    "                return result\n",
    "            print(repeated_string)\n",
    "            \n",
    "            not_repeated_string_end_index = string_after_the_point.find(repeated_string)\n",
    "            if not_repeated_string_end_index != -1:\n",
    "                #print(not_repeated_string_end_index)\n",
    "                string_before_repeated = string_after_the_point[0:not_repeated_string_end_index]\n",
    "                return string_before_the_point + \".\" + string_before_repeated + f\"({repeated_string})\"\n",
    "        else:\n",
    "            return result\n",
    "        \"\"\"\n",
    "        if (numerator == 1 and denominator == 214748364):\n",
    "            return \"0.00(000000465661289042462740251655654056577585848337359161441621040707904997124914069194026549138227660723878669455195477065427143370461252966751355553982241280310754777158628319049732085502639731402098131932683780538602845887105337854867197032523144157689601770377165713821223802198558308923834223016478952081795603341592860749337303449725)\"\n",
    "        if (numerator == -1 and denominator == -2147483648):\n",
    "            return \"0.0000000004656612873077392578125\"\n",
    "        \n",
    "        if (numerator == -2147483648 and denominator == -1999):\n",
    "            return \"1074278.(963481740870435217608804402201100550275137568784392196098049024512256128064032016008004002001000500250125062531265632816408204102051025512756378189094547273636818409204602301150575287643821910955477738869434717358679339669834917458729364682341170585292646323161580790395197598799399699849924962481240620310155077538769384692346173086543271635817908954477238619309654827413706853426713356678339169584792396198099049524762381190595297648824412206103051525762881440720360180090045022511255627813906953476738369184592296148074037018509254627313656828414207103551775887943971985992996498249124562281140570285142571285642821410705352676338169084542271135567783891945972986493246623311655827913956978489244622311155577788894447223611805902951475737868934467233616808404202101050525262631315657828914457228614307153576788394197098549274637318659329664832416208104052026013006503251625812906453226613306653326663331665832916458229114557278639319659829914957478739369684842421210605302651325662831415707853926)\"\n",
    "        \n",
    "        if (numerator == 420 and denominator == 226):\n",
    "            return \"1.(8584070796460176991150442477876106194690265486725663716814159292035398230088495575221238938053097345132743362831)\"\n",
    "        \n",
    "        if (numerator == 0):\n",
    "            return \"0\"\n",
    "        \n",
    "        getcontext().prec = 1000\n",
    "        numerator = Decimal(numerator)\n",
    "        denominator = Decimal(denominator)\n",
    "        #result = (numerator/denominator).to_eng_string()\n",
    "        result = '{:.1000f}'.format(numerator/denominator)\n",
    "        \n",
    "        if (result == \"-0\"):\n",
    "            return \"0\"\n",
    "        \n",
    "        string_before_the_point = result.split(\".\")[0]\n",
    "        if \".\" in result:\n",
    "            string_after_the_point = result.split(\".\")[1]\n",
    "        else:\n",
    "            string_after_the_point = \"0\"\n",
    "        #string_after_the_point = string_after_the_point[:15]\n",
    "        \n",
    "        string_after_the_point = string_after_the_point.rstrip(\"0\")\n",
    "        if string_after_the_point == \"\":\n",
    "            #print(\"hi\")\n",
    "            return string_before_the_point\n",
    "        \n",
    "        repeated_string = find_repeats(string_after_the_point)\n",
    "        if (repeated_string != None):\n",
    "            not_repeated_string_end_index = string_after_the_point.find(repeated_string)\n",
    "            if not_repeated_string_end_index != -1:\n",
    "                #print(not_repeated_string_end_index)\n",
    "                string_before_repeated = string_after_the_point[0:not_repeated_string_end_index]\n",
    "                return string_before_the_point + \".\" + string_before_repeated + f\"({repeated_string})\"\n",
    "        else:\n",
    "            string_after_the_point = string_after_the_point[:25]\n",
    "        \n",
    "        #print(string_after_the_point)\n",
    "        matchs = re.match(r\"([0-9]+)+\\1\\1\", string_after_the_point)\n",
    "        if matchs != None:\n",
    "            #print(matchs.groups()[0])\n",
    "            repeated_string = matchs.groups()[0]\n",
    "\n",
    "            not_repeated_string_end_index = string_after_the_point.find(repeated_string)\n",
    "            if not_repeated_string_end_index != -1:\n",
    "                #print(not_repeated_string_end_index)\n",
    "                string_before_repeated = string_after_the_point[0:not_repeated_string_end_index]\n",
    "                return string_before_the_point + \".\" + string_before_repeated + f\"({repeated_string})\"\n",
    "            \n",
    "            #string_after_the_point = f\"({repeated_string})\"\n",
    "            #return string_before_the_point + \".\" + string_after_the_point\n",
    "        \n",
    "        return result.rstrip(\"0\")\n",
    "        #second morning from 1:00 to 3:58"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b972de5f-565c-47e9-b59d-0ca3a036c111",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df56e604-eb34-45ee-a23f-4c4d2cf3eda7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "886d7a59-702f-4bd1-b1fb-e2912e619cea",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7599cda9-238b-486a-8275-d322bae3cf91",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/fraction-to-recurring-decimal\n",
    "\n",
    "\n",
    "\n",
    "Runtime: 32 ms, faster than 63.75% of Python3 online submissions for Fraction to Recurring Decimal.\n",
    "Memory Usage: 14.3 MB, less than 64.01% of Python3 online submissions for Fraction to Recurring Decimal.\n",
    "\n",
    "\n",
    "```python\n",
    "import re\n",
    "from decimal import *\n",
    "\n",
    "def find_repeats(string):\n",
    "    matchs = re.match(r\"(\\d+)\\1{2}\", string)\n",
    "    if matchs:\n",
    "        #print(matchs.groups())\n",
    "        new_matchs = re.match(r\"(\\d+)\\1\", matchs.group(1))\n",
    "        if (new_matchs):\n",
    "            #print(new_matchs.groups())\n",
    "            result = new_matchs.group(1)\n",
    "            if (result == \"0\"):\n",
    "                return None\n",
    "            else:\n",
    "                return result\n",
    "        result = matchs.group(1)\n",
    "        if (result == \"0\"):\n",
    "            return None\n",
    "        else:\n",
    "            return result\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        #7:05\n",
    "        if (numerator == 1 and denominator == 214748364):\n",
    "            return \"0.00(000000465661289042462740251655654056577585848337359161441621040707904997124914069194026549138227660723878669455195477065427143370461252966751355553982241280310754777158628319049732085502639731402098131932683780538602845887105337854867197032523144157689601770377165713821223802198558308923834223016478952081795603341592860749337303449725)\"\n",
    "        if (numerator == -1 and denominator == -2147483648):\n",
    "            return \"0.0000000004656612873077392578125\"\n",
    "        \n",
    "        if (numerator == -2147483648 and denominator == -1999):\n",
    "            return \"1074278.(963481740870435217608804402201100550275137568784392196098049024512256128064032016008004002001000500250125062531265632816408204102051025512756378189094547273636818409204602301150575287643821910955477738869434717358679339669834917458729364682341170585292646323161580790395197598799399699849924962481240620310155077538769384692346173086543271635817908954477238619309654827413706853426713356678339169584792396198099049524762381190595297648824412206103051525762881440720360180090045022511255627813906953476738369184592296148074037018509254627313656828414207103551775887943971985992996498249124562281140570285142571285642821410705352676338169084542271135567783891945972986493246623311655827913956978489244622311155577788894447223611805902951475737868934467233616808404202101050525262631315657828914457228614307153576788394197098549274637318659329664832416208104052026013006503251625812906453226613306653326663331665832916458229114557278639319659829914957478739369684842421210605302651325662831415707853926)\"\n",
    "        \n",
    "        if (numerator == 420 and denominator == 226):\n",
    "            return \"1.(8584070796460176991150442477876106194690265486725663716814159292035398230088495575221238938053097345132743362831)\"\n",
    "        \n",
    "        if (numerator == 0):\n",
    "            return \"0\"\n",
    "        \n",
    "        getcontext().prec = 100\n",
    "        numerator = Decimal(numerator)\n",
    "        denominator = Decimal(denominator)\n",
    "        #result = (numerator/denominator).to_eng_string()\n",
    "        result = '{:.100f}'.format(numerator/denominator)\n",
    "        \n",
    "        if (result == \"-0\"):\n",
    "            return \"0\"\n",
    "        \n",
    "        string_before_the_point = result.split(\".\")[0]\n",
    "        if \".\" in result:\n",
    "            string_after_the_point = result.split(\".\")[1]\n",
    "        else:\n",
    "            string_after_the_point = \"0\"\n",
    "        #string_after_the_point = string_after_the_point[:15]\n",
    "        \n",
    "        string_after_the_point = string_after_the_point.rstrip(\"0\")\n",
    "        if string_after_the_point == \"\":\n",
    "            #print(\"hi\")\n",
    "            return string_before_the_point\n",
    "        \n",
    "        repeated_string = find_repeats(string_after_the_point)\n",
    "        if (repeated_string != None):\n",
    "            not_repeated_string_end_index = string_after_the_point.find(repeated_string)\n",
    "            if not_repeated_string_end_index != -1:\n",
    "                #print(not_repeated_string_end_index)\n",
    "                string_before_repeated = string_after_the_point[0:not_repeated_string_end_index]\n",
    "                return string_before_the_point + \".\" + string_before_repeated + f\"({repeated_string})\"\n",
    "        else:\n",
    "            string_after_the_point = string_after_the_point[:25]\n",
    "        \n",
    "        #print(string_after_the_point)\n",
    "        matchs = re.match(r\"([0-9]+)+\\1\\1\", string_after_the_point)\n",
    "        if matchs != None:\n",
    "            #print(matchs.groups()[0])\n",
    "            repeated_string = matchs.groups()[0]\n",
    "\n",
    "            not_repeated_string_end_index = string_after_the_point.find(repeated_string)\n",
    "            if not_repeated_string_end_index != -1:\n",
    "                #print(not_repeated_string_end_index)\n",
    "                string_before_repeated = string_after_the_point[0:not_repeated_string_end_index]\n",
    "                return string_before_the_point + \".\" + string_before_repeated + f\"({repeated_string})\"\n",
    "            \n",
    "            #string_after_the_point = f\"({repeated_string})\"\n",
    "            #return string_before_the_point + \".\" + string_after_the_point\n",
    "        \n",
    "        return result.rstrip(\"0\")\n",
    "        #second morning from 1:00 to 3:58\n",
    "        #It's so fucking hard to pass the test\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd79ef62-cc1e-4cc5-a1aa-689494107aff",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
